
include_javaScript(url_prefix+'editor/ckeditor/ckeditor.js');
include_javaScript(url_prefix+'editor/ckfinder/ckfinder.js');


$(function() {
    $("#tabs").tabs();
});
  
$(document).ready(function() { 
    
    hs.graphicsDir = url_base+'app/admin/view/temp/default/layout/css/highslide/graphics/';
    hs.align = 'center';
    hs.closeText = '<img border="0" src="'+url_base+'app/admin/view/temp/default/layout/css/highslide/graphics/close_X.png" />';
    hs.creditsText = 'this.a.title';
    hs.outlineType = 'rounded-white';
    hs.showCredits = false;
    hs.wrapperClassName = 'draggable-header';
    hs.cacheAjax = false;
    hs.dimmingOpacity = 0.7;
    hs.onDimmerClick = function() {
        return false;
    }
         
    $('#btn-submit').click(function() {
        var hasError = false;
        var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
    
        var emailaddressVal = $("#Email").val();
        if(emailaddressVal == '') {
            $(".error").show().text(_['error-emal-empty']);
            hasError = true;
        }
    
        else if(!emailReg.test(emailaddressVal)) {
            $(".error").show().text(_['error-emal-no-format']);
            hasError = true;
        }
    
        if(hasError == true) {
            return false;
        }
    
    });
    
    $('input:checkbox[name="delId[]"]').click(function() { 
    
        if( $(this).attr("checked")== "checked"){
        
            $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);
        
        }else{
        
            if(getValCb().length>0)
            {               
                $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);
            }else{
                $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled",true);   
            }
        }
    });
});

function getValCb()
{
    var delId = [];
    $('input:checkbox[name="delId[]"]:checked')
    .each( function(index) { 
        delId.push($(this).val());
    });
	
    return delId;
}

function getValCbCopy()
{
    var delId = [];
    $('input:checkbox[name="delIdC[]"]:checked')
    .each( function(index) { 
        delId.push($(this).val());
    });
	
    return delId;
}

function checkAllCopy()
{
    if(getValCbCopy().length>0){        
        $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled",true);        
        $('input:checkbox[name="all_copy"]').removeAttr("checked");
        $('input:checkbox[name="delIdC[]"]').removeAttr("checked");
        
    }else{
	   
        $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);        
        $('input:checkbox[name="all_copy"]').attr("checked","checked");
        $('input:checkbox[name="delIdC[]"]').attr("checked","checked");
    }	
}

function checkAll()
{
    if(getValCb().length>0){        
        $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled",true);        
        $('input:checkbox[name="all"]').removeAttr("checked");
        $('input:checkbox[name="delId[]"]').removeAttr("checked");
        
    }else{
	    
        $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);        
        $('input:checkbox[name="all"]').attr("checked","checked");
        $('input:checkbox[name="delId[]"]').attr("checked","checked");
    }	
}

function SaveCopy()
{
    var id_all = getValCbCopy();
    if(id_all!="")
    {   
        if (confirm(_['ban_co_thuc_sua_muon_copy'])) 
        {                        
            var url = _seokey[0] + '/' + _seokey[1]+ "/ajax/savecopy/"+id_all+"/"+_seokey[3];               
            $.ajax({ 
                type    : "GET", 
                url     : url_base+url, 
                cache   : false,
                dataType: "html",
                success : function(DataHtml)
                {         
                    if (confirm(_['save_ok'])) 
                    { 
                        $("#div_html").html(DataHtml); 
                        window.parent.hs.close();
                    }
                } 
            });
        }
    }
    else
    {
        alert(_['phaichon']);
    } 
}

function checkOne()
{
    $('input:checkbox[name="delId[]"]').click(function() { 
    
        if( $(this).attr("checked")== "checked"){

            $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);

        }else{

            if(getValCb().length>0)
            {               
                $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);
            }else{
                $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled",true);   
            }
        }
    });
}

function checkOneCopy()
{
    $('input:checkbox[name="delIdC[]"]').click(function() { 
    
        if( $(this).attr("checked")== "checked"){

            $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);

        }else{

            if(getValCb().length>0)
            {               
                $('.but_tool').removeClass('butx').addClass('but_login').attr("disabled",false);
            }else{
                $('.but_tool').removeClass('but_login').addClass('butx').attr("disabled",true);   
            }
        }
    });
}
function LoadPosition(url,id)
{
    var url = url+"/ajax/loadposition/"+id+"/";   
    $.ajax({ 
        type    : "GET", 
        url     : url_base+url, 
        cache   : false,
        dataType: "html",
        success : function(DataHtml)
        {    
            $("#div_html").html(DataHtml);   
            checkOne()
        } 
    });
}

function loadDataCopy()
{   
    var id_copy =0, base_url ;
    /*get com source*/
    id_copy = $("#select_copy").val(); 
    
    base_url = _seokey[0] + '/' + _seokey[1];
   
    var url = base_url + "/ajax/loaddatacopy/"+id_copy;
    $.ajax({ 
        type    : "GET", 
        url     : url_base+url, 
        cache   : false,
        dataType: "html",            
        success : function(DataHtml)
        {   
            if(DataHtml!=false)
            {
                $("#sortable_copy").html(DataHtml);  
                 checkOneCopy()
                //set an hien
                $("#table_data").removeClass("disabled");
            }
            else
            {
                $("#table_data").addClass("disabled");                
            }           
        } 
    });
}

function loadData(base_url , id)
{
    if(id == 0)
    {
        var id_copy ;
        id_copy= $("#sel_copy").val();      
    }
    var x=document.getElementById("listposition");
    var value = x.options[x.selectedIndex].text;
    var url = base_url + "/ajax/loaddata/"+value+"/"+id;
   
    $.ajax({ 
        type    : "GET", 
        url     : url_base+url, 
        cache   : false,
        dataType: "html",            
        success : function(DataHtml)
        {   
            if(DataHtml!=false)
            {
                $("#module").html(DataHtml);                   
                //set an hien
                $("#listdata").removeClass("disabled");
                $("#Save").removeClass("disabled");
            }
            else
            {
                $("#listdata").addClass("disabled");                
                $("#Save").addClass("disabled");
            }           
        } 
    });
}

function disSave()
{
    var x=document.getElementById("Save");
    x.setAttribute("disabled", "disabled")
}

function change_Status(bs_url,position,id,comID,status) {
    var tt ='';
    if(status == 'true')
    {
        tt = _['hien-thi']; 
    }
    else
    {
        tt = _['an'];
    }
        
    if (confirm("Bạn có muốn "+tt+" module này không")) 
    {
        var url = bs_url+ "/ajax/change/"+status+"/"+position+'/'+id+"/"+comID;
   
        $.ajax({ 
            type    : "GET", 
            url     : url_base+url, 
            cache   : false,
            dataType: "html",
            success : function(DataHtml)
            { 
                $("#div_html").html(DataHtml);   
            } 
        });
    }
}
function delete_module(bs_url,position,id,comID,status) {
    if (confirm("Bạn có muốn xóa module này không")) 
    {
        //admin/he-thong-quan-ly-vi-tri-module/ajax/delete/content_header/2/10
        var url = bs_url+ "/ajax/delete/"+position+'/'+id+"/"+comID;
   
        $.ajax({ 
            type    : "GET", 
            url     : url_base+url, 
            cache   : false,
            dataType: "html",
            success : function(DataHtml)
            { 
                $("#div_html").html(DataHtml);   
            //$(e).attr("class", value)
            } 
        });
    }
}

function active_bv(bs_url,status,page) {
    var tt ='';
    if(status == 'true')
    {
        tt = _['hien-thi']; 
    }
    else
    {
        tt = _['an'];
    }
           
    var id_all = getValCb();
      
    if(id_all!="")
    {       
        if (confirm("Bạn có muốn "+tt+" module này không")) 
        {            
            var url = bs_url+ "/ajax/changeall/"+status+"/"+id_all;               
            $.ajax({ 
                type    : "GET", 
                url     : url_base+url, 
                cache   : false,
                dataType: "html",
                success : function(DataHtml)
                {
                    checkAll()
                    $("#div_html").html(DataHtml);  
                    checkOne()
                } 
            });
        }
    }
    else
    {
        alert("phải chọn 1 module");
    }  
}
function del_bv(bs_url,id) {
    
    var id_all = getValCb(id);
    //alert(id_all);
    if(id_all!="")
    {
        if (confirm("Bạn có muốn xóa module này không")) 
        {
            var url = bs_url+ "/ajax/deleteall/"+id_all;
            $.ajax({ 
                type    : "GET", 
                url     : url_base+url, 
                cache   : false,
                dataType: "html",
                success : function(DataHtml)
                { 
                    $("#div_html").html(DataHtml);    
                    checkOne()
                } 
            });
        }
    }   
    else
    {
        alert("phải chọn 1 module");
    }
} 

function setOrder()
{
    var str ='';
    var numTag =  $("#sortable>tr").length;
    if(numTag>0)
    {                      
        var cur_id ='';
        for(var i=0; i<numTag; i++)        {                       
            cur_id = $("#sortable>tr:eq("+i+")>td:eq(0) input").val();
            str +=","+cur_id;
           
            $("#"+cur_id+">td:eq(3)>span:eq(1)").text(i+1);
        }
    }    
    return str;
}
function setOrderAjax(ID)
{
    url= _seokey[0] + '/' + _seokey[1];
    var url = url+ "/ajax/order/"+ID+"/";
    $.ajax({ 
        type    : "GET", 
        url     : url_base+url, 
        cache   : false
        
    });
}

function processChecked(active)
{
    var StrID = getValCb();
    switch (active)
    {
        case 'true':
            var strData = _seokey[0]+"/"+_seokey[1]+"/ajax/"+active+"/"+StrID;
            var sms = _['showx'];
            var smsError=_['error_show'];
            var errorNotOK = _['error_ok_show'];
            var status = _['show'];
            var activea = 'false';
            var activeb = 'del';
            break;
		
        case 'false':
            var strData = _seokey[0]+"/"+_seokey[1]+"/ajax/"+active+"/"+StrID;
            var sms = _['hidex'];
            var smsError=_['error_hide'];
            var errorNotOK = _['error_ok_hide'];
            var status = _['hide'];
            var activea = 'true';
            var activeb = 'del';
            break;

        case 'delete':
            var strData = _seokey[0]+"/"+_seokey[1]+"/ajax/"+active+"/"+StrID;
            var sms = _['delete'];
            var smsError=_['error_delete'];
            var errorNotOK = _['error_ok_delete'];
            var deletex = 'ok';
            break;        
    }	
    if(StrID!=""){
        var smsCheck = confirm(sms);
        if(smsCheck==true){
            $.ajax({ 
                type: "POST", 
                url: url_base+""+strData, 
                cache: false,
                dataType: "html",
                success: function(DataHtml){ 
                    if(DataHtml=='ok')
                    {
                        for(x in StrID)
                        {
                            if(deletex)
                            {
                                $('.tr'+StrID[x]).remove();                                
                            }
                            else{
                         
                                $('.status'+StrID[x]).text(status);
                            
                                if($('.tr'+StrID[x]).hasClass('class_'+activea) )
                                    $('.tr'+StrID[x]).removeClass('class_'+activea);
                                if($('.tr'+StrID[x]).hasClass('class_'+activeb) )
                                    $('.tr'+StrID[x]).removeClass('class_'+activeb);
                            
                                $('.tr'+StrID[x]).addClass('class_'+active);
                            }
                        
                        }
                    }
                    else{
                        alert(errorNotOK);                   
                    
                    }
				 
                } 
            });
        }
	
    }else{
        alert(smsError);
	
    }
}
